package com.simba.server.commands.impl.record;

import com.simba.common.command.ICommand;
import com.simba.common.database.DbOperator;
import com.simba.common.log.LogUtil;
import com.simba.server.base.CommonUtil;
import com.simba.server.components.SortCommonDefines;
import com.simba.server.components.data.BoxInfo;
import com.simba.server.components.data.SlotInfo;
import org.apache.log4j.Logger;

public class RecordBoxPackCmd implements ICommand
{
	public static final String NAME = "RecordBoxPackCmd";
	private static final Logger logger = Logger.getLogger(RecordBoxPackCmd.class);
	private final BoxInfo boxInfo;
	private final SlotInfo slotInfo;
	private final String sql = "insert into tt_slot_pack_info("
			+ "slotId, boxCode, boxType, createTime, mailCount,uploadFlag,planId,roadSlot,printerMaster,printerSlave)"
			+ " values(?,?,?,?,?,?,?,?,?,?);";

	public RecordBoxPackCmd(SlotInfo slotInfo)
	{
		this.slotInfo = slotInfo;
		this.boxInfo = this.slotInfo.getBoxInfo();
	}

	@Override
	public String getName()
	{
		return RecordBoxPackCmd.NAME;
	}

	@Override
	public Boolean run(Object params)
	{
		boolean ret;

		if (!(params instanceof DbOperator))
		{
			return false;
		}

		try
		{
			DbOperator dbOperator = (DbOperator) params;

			dbOperator.prepareStatement(this.sql);
			dbOperator.setString(1, CommonUtil.doNullData(boxInfo.getSlotId(), "none"));
			dbOperator.setString(2, CommonUtil.doNullData(boxInfo.getBoxCode(), "none"));
			dbOperator.setString(3, CommonUtil.doNullData(boxInfo.getBoxType(), "none"));
			dbOperator.setString(4, CommonUtil.doNullData(boxInfo.getCreateTime(), "none"));
			dbOperator.setInt(5, this.boxInfo.getMails().size());
			dbOperator.setInt(6, SortCommonDefines.UPLOAD_DEFAULT_FLAG);
			dbOperator.setString(7, CommonUtil.doNullData(boxInfo.getPlanId(), "none"));
			dbOperator.setString(8, CommonUtil.doNullData(boxInfo.getRoadSlot(), "none"));
			dbOperator.setString(9, CommonUtil.doNullData(slotInfo.getMasterPrinterIp(), "none"));
			dbOperator.setString(10, CommonUtil.doNullData(slotInfo.getSlavePrinterIp(), "none"));

			int res = dbOperator.executeUpdate();
			if (res > 0)
			{
				ret = true;
			}
			else
			{
				ret = false;
			}
		}
		catch (Exception ex)
		{
			LogUtil.LogError(RecordBoxPackCmd.logger, "error:"+ex.getMessage());
			ret = false;
		}

		return ret;
	}
}
